package com.biao.transaction.example.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.biao.transaction.example.entity.OperateA;
import com.biao.transaction.example.mapper.OperateAMapper;
import com.biao.transaction.example.service.OperateAService;
import com.biao.transaction.example.service.OperateBService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenwenbiao
 * @since 2020-04-23
 */
@Service
@Slf4j
public class OperateAServiceImpl extends ServiceImpl<OperateAMapper, OperateA> implements OperateAService {

    @Autowired
    private OperateAMapper operateAMapper;

    @Autowired
    private OperateBService operateBService;

    @Override
    public void aNoTransationBNoTransation(String name) {
        OperateA operateA = new OperateA(name);
        save(operateA);

        operateBService.noTransactionThrowException("从A内部添加到B记录");
    }





    @Transactional
    @Override
    public void aTransationBNoTransation(String name) {
        OperateA operateA = new OperateA(name);
        save(operateA);

        operateBService.noTransactionThrowException("从A内部添加到B记录");
    }

    @Transactional
    @Override
    public void aTransationBNoTransation2(String name) {
        OperateA operateA = new OperateA(name);
        save(operateA);
        operateBService.noTransaction("从A内部添加到B记录");

        int x = 1 / 0;
    }



    @Transactional
    @Override
    public void aTransationBNoTransationWithTryCatch(String name) {
        OperateA operateA = new OperateA(name);
        save(operateA);

        try {
            operateBService.noTransactionThrowException("从A内部添加到B记录");
        }catch (Exception e){
            log.error("exception", e);
        }
    }

    @Transactional
    @Override
    public void aTransationBTransation(String name) {
        OperateA operateA = new OperateA(name);
        save(operateA);

        operateBService.hasTransactionThrowException("从A内部添加到B记录");
    }


    @Override
    public void aNoTransationBTransation(String name) {
        OperateA operateA = new OperateA(name);
        save(operateA);

        operateBService.hasTransactionThrowException("从A内部添加到B记录");
    }

    @Override
    public void aNoTransationBTransation2(String name) {
        OperateA operateA = new OperateA(name);
        save(operateA);

        operateBService.hasTransaction("从A内部添加到B记录");
        int x = 1 / 0;
    }


    @Transactional
    @Override
    public void aTransationBTransationWithTryCatch(String name) {
        OperateA operateA = new OperateA(name);
        save(operateA);

        try {
            operateBService.hasTransactionThrowException("从A内部添加到B记录");
        }catch (Exception e){
            log.error("exception", e);
        }
    }


    @Transactional
    @Override
    public void aTransationBNewTransation(String name) {
        OperateA operateA = new OperateA(name);
        save(operateA);

        try {
            operateBService.hasNewTransactionThrowException("从A内部添加到B记录");
        }catch (Exception e){
            log.error("exception", e);
        }
    }


    @Transactional
    @Override
    public void aTransationANewTransation(String name) {
        OperateA operateA = new OperateA(name);
        save(operateA);

        try {
            aNewTransation(name);
        }catch (Exception e){
            log.error("exception", e);
        }
    }


    @Transactional
    @Override
    public void aTransationANewTransationRightCall(String name) {
        OperateA operateA = new OperateA(name);
        save(operateA);

        OperateAService operateAService = (OperateAService) AopContext.currentProxy();
        try {
            operateAService.aNewTransation(name);
        }catch (Exception e){
            log.error("exception", e);
        }
    }


    @Transactional(propagation = Propagation.REQUIRES_NEW)
    @Override
    public void aNewTransation(String name) {
        OperateA operateA = new OperateA(name);
        save(operateA);

        int x = 1 / 0;
    }
}
